﻿Imports System.Data

Partial Class CDetallesCelular
    Inherits System.Web.UI.Page
    'instanciar la clase operador
    Dim operador As New Operador
    Dim Equipo As New Equipo

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Master.MostrarClienteSession()

        'si la pagina no ha ido al servidor
        If Not Page.IsPostBack Then

            'evaluo: si no existe cooie producto, regresar
            If IsNothing(Request.Cookies("equipo")) Then
                Response.Redirect("Catalogo.aspx")
            Else

                Dim strMoneda As String = CType(Session.Item("cboMoneda"), String)

                Dim identificador As String = ""

                If strMoneda = Nothing Then
                    identificador = "S/. "
                ElseIf strMoneda = "02" Then
                    identificador = "$. "
                ElseIf strMoneda = "01" Then
                    identificador = "S/. "
                End If

                lblIden.Text = identificador

                With Request.Cookies("equipo")
                    lblCodigo.Text = .Values("cod")
                    lblMar.Text = .Values("mar")
                    lblMod.Text = .Values("mod")
                    lblTip.Text = .Values("tip")
                    lblPre.Text = .Values("pre")
                    'Imagen
                    Image1.ImageUrl = "cellFotos/" + lblCodigo.Text + ".png"
                    cargarColoresDisponibles()
                End With
            End If

        End If
    End Sub
    Sub cargarColoresDisponibles()
        Dim codigo As String = Request.Cookies("equipo").Values("cod")
        Dim dt As DataTable = Equipo.ListadoParametro(" select  co.cod_color cod ,desc_color as 'desc' " & _
                                                        " from dbo.coloresCantidad as cc inner join " & _
                                                        " color as co on " & _
                                                        " cc.cod_color = co.cod_color " & _
                                                        " where cc.cod_equipo = " & codigo)
        cboColores.DataSource = dt
        cboColores.DataTextField = "desc"
        cboColores.DataValueField = "cod"
        cboColores.DataBind()
        cboColores.Items.Insert(0, "Seleccione")

    End Sub
    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        'definir la referencia de Session carrito

        Dim tb As DataTable = CType(Session("carrito"), DataTable)

       

        'validar campos

        Dim validar As Boolean = validarcampos()


        If validar = True Then
            'buscar el codigo en los registros
            Dim reg As DataRow = tb.Rows.Find(lblCodigo.Text)
            If IsNothing(reg) Then
                reg = tb.NewRow
                reg("codEqui") = lblCodigo.Text
                reg("modelo") = lblMod.Text
                reg("color") = cboColores.SelectedValue
                '  reg("codOpe") = cboOperador.SelectedValue
                Dim strMoneda As String = CType(Session.Item("cboMoneda"), String)

                If strMoneda = Nothing Then
                    reg("precio") = Val(lblPre.Text)
                ElseIf strMoneda = "02" Then
                    reg("precio") = Val(lblPre.Text) * CDbl(Session.Item("Cambio"))
                ElseIf strMoneda = "01" Then
                    reg("precio") = Val(lblPre.Text)
                End If

                reg("cantidad") = Val(txtCant.Text)

                'agregar reg a tb
                tb.Rows.Add(reg)
                'MsgBox("Equipo Agregado")

                Mensaje1.mostrarMensaje(1, "Los Datos de la Persona fueron Registrados Correctamente")


                Dim SumaTotal As Double = 0
                Dim cantItem As Integer = 0
                For i As Integer = 0 To tb.Rows.Count - 1

                    SumaTotal = SumaTotal + CDbl(tb.Rows(i).Item(6).ToString())
                    cantItem = cantItem + CType(tb.Rows(i).Item(5).ToString(), Integer)

                Next

                Session.Add("TotalItemCarrito", cantItem)
                Session.Add("TotalSolesCarrito", SumaTotal)

                Response.Redirect("Catalogo.aspx")

            Else 'si lo encontro
                MsgBox("No se puede agregar 2 veces el mismo equipo")
                ' Response.Redirect("Catalogo.aspx")

            End If
        Else

        End If
        'si reg esta vacio, significa que es nuevo registro
        
    End Sub

    Function validarcampos() As Boolean
        Dim validar As Boolean = True

        If txtCant.Text = "" Or txtCant.Text = "0" Then
            MsgBox("El campo cantidad es obligatorio")
            validar = False

        ElseIf cboColores.SelectedValue.ToString = "Seleccione" Then
            MsgBox("El campo color es obligatorio")
            validar = False
        End If
        Return validar
    End Function

    Protected Sub btnRegresar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegresar.Click
        Response.Redirect("Catalogo.aspx")
    End Sub



    Protected Sub btnResumen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnResumen.Click
        Response.Redirect("CarritoResumen.aspx")
    End Sub

    Protected Sub txtCant_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCant.TextChanged
        Dim strcant As String = txtCant.Text
        If strcant.Trim <> "" Then

            Dim total As Double = CType(txtCant.Text, Integer) * CDbl(lblPre.Text)

            divTotal.Style("display") = "block"
            divTotal.Style("visibility") = "visible"
            lblTotalPagar.Text = "Total: "
            lbltotal.Text = lblIden.Text & CType(Format(total, "###,###,##0.00"), String)

            upDatos.Update()

        End If
    End Sub
End Class
